package com.example.treeEngine.impl;

import com.example.pojo.mysql.SysDept;
import com.example.pojo.mysql.SysMenu;
import com.example.treeEngine.AbstractTree;

import java.util.ArrayList;
import java.util.List;

/* ━━━━━━佛祖保佑━━━━━━
 *                  ,;,,;
 *                ,;;'(    社
 *      __      ,;;' ' \   会
 *   /'  '\'~~'~' \ /'\.)  主
 * ,;(      )    /  |.     义
 *,;' \    /-.,,(   ) \    码
 *     ) /       ) / )|    农
 *     ||        ||  \)
 *     (_\       (_\
 * ━━━━━━永无BUG━━━━━━
 * @author ：zjq
 * @date ：2020/12/10 16:56
 * @description: TODO
 * @version: V1.0
 * @slogan: 天下风云出我辈，一入代码岁月催
 */
public class MenuTreeImpl extends AbstractTree<SysMenu> {


    @Override
    protected List<SysMenu> buildTree(List<SysMenu> inputList) {

        List<SysMenu> returnList = new ArrayList();
        inputList.stream().forEach(d -> {
            // 顶级 节点 遍历所有子节点
            if (d.getParentId() == 0L) {
                recursionFn(inputList, d);
                returnList.add(d);
            }
        });
        return returnList;
    }

    @Override
    protected void recursionFn(List<SysMenu> inputList, SysMenu sysMenu) {
        List<SysMenu> childList = getChildList(inputList, sysMenu);
        sysMenu.setChildren(childList);
        childList.stream().forEach(n -> {
            if (hasChild(inputList, n)) {
                recursionFn(inputList, n);
            }

        });
    }

    @Override
    protected List<SysMenu> getChildList(List<SysMenu> inputList, SysMenu sysMenu) {
        List<SysMenu> tmpList = new ArrayList();
        inputList.stream().forEach(n -> {
            if (n.getParentId() != null && n.getParentId().longValue() == sysMenu.getMenuId().longValue()) {
                tmpList.add(n);
            }
        });
        return tmpList;
    }

    @Override
    protected boolean hasChild(List<SysMenu> inputList, SysMenu sysMenu) {
        return getChildList(inputList, sysMenu).size() > 0 ? true : false;
    }
}
